package com.zzuli.yxy.controller;

import com.zzuli.yxy.entity.Menu;
import com.zzuli.yxy.enumeration.AppHttpCodeEnum;
import com.zzuli.yxy.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzuli.yxy.service.MenuService;

import javax.servlet.http.HttpServletResponse;

/**
 * @auther pony
 * @create 2023-03-30 19:11
 */
@RestController
@RequestMapping("menu")
@CrossOrigin(origins = "*")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取全部权限信息
     * @return
     */
    @GetMapping("getAllMenu")
    public R getAllRole(){
        return menuService.getAllMenu();
    }

    /**
     * 获取权限管理分页信息
     * @param pageCurrent 当前页
     * @param pageSize 每页条数
     * @param name 关键字 非必传
     * @return
     */
    @GetMapping({"/page/{pageCurrent}/{pageSize}",
            "/page/{pageCurrent}/{pageSize}/{name}"})
    public R getPage(@PathVariable("pageCurrent") Integer pageCurrent,
                     @PathVariable("pageSize") Integer pageSize,
                     @PathVariable(name = "name", required = false) String name){
        return menuService.getPage(pageCurrent, pageSize, name);
    }

    /**
     * 更改权限当前状态
     * @param status 状态码 0：正常 1：异常
     * @return
     */
    @GetMapping("/status/{id}/{status}")
    public void setStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status){
        menuService.setStatus(id, status);
    }

    /**
     * 根据权限id，查询单个权限信息
     * @param id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public R selectOneById(@PathVariable("id") Integer id){
        return menuService.selectOneById(id);
    }

    /**
     * 修改权限信息
     * @param menu
     * @return
     */
    @PutMapping("/update")
    public R updateMenu(@RequestBody Menu menu){
        //非空判断
        if ("".equals(menu.getMenu())||menu.getMenu()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return menuService.updateMenu(menu);
    }

    /**
     * 权限信息 -- 导出 Excel
     * @param response
     * @return
     */
    @GetMapping("outExcel")
    public void outExcel(HttpServletResponse response){
        menuService.outExcel(response);
    }
}
